﻿@using System.Text
@using Ganss.XSS
@using WebGrease.Css.Extensions
@{
    ViewBag.Title = "编辑新闻";
    var news = ViewBag.news as HynuIOTA.Entity.News;

    var sanitizer = new HtmlSanitizer();

    string channel = string.Empty;
    if (news.Channel.Any())
    {
        StringBuilder channelsSbBuilder = new StringBuilder();
        news.Channel.ForEach(n => channelsSbBuilder.Append($"{n.Id},"));
        channel = channelsSbBuilder.ToString().Substring(0, channelsSbBuilder.Length - 1);
    }
}
@section scripts
{
    <script src="~/kindeditor/kindeditor-all-min.js"></script>
    <script src="~/kindeditor/lang/zh-CN.js"></script>
    <script>
        function doBegin() {
           window.top.$.msgBoxObj.showMsgInfo('正在提交数据....');
        }

        function doFail() {
            window.top.$.msgBoxObj.showMsgErr('网络异常!');
        }

        function doComplete(xhrObj) {
            $.processJsonMsg(xhrObj.responseText, function(response) {
                if (response.Statu === 1) {
                    window.top.topHelper.closeComWindow();
                    window.top.topHelper.updateDataGrid();
                }
            });
        }

        // 初始化控件
        function initControl() {
            $('#Channels').tagbox({
                hasDownArrow: true,
                limitToList: true,
                valueField: 'id',
                textField: 'text',
                url: '@Url.Action("GetChannelCombox", "Channel", new {area = "Admin"})',
                loadFilter: function(data) {
                    return data.Data;
                }
            });
        }

        $(function() {
            initControl();
        });
        //初始化编辑器
        KindEditor.ready(function(K) {
            var options = {
                cssPath: '@Url.Content("~/kindeditor/themes/qq/qq.css")',
                uploadJson: '@Url.Action("UploadFile", "File", new {area = "Media"})',
                fileManagerJson: '@Url.Action("FileManager", "File", new {area = "Media"})',
                resizeType: 1, // 只能改变高度
                allowFileManager: true,
                filterMode: true,
                afterBlur: function() { editor.sync(); },
                afterUpload: function(url, data, name) {
                    console.log(url);
                    console.log(data);
                    console.log(name);
                }
            };
            window.editor = K.create('#Content', options);
            // 弹出缩略图
            K('#form-upimg').click(function() {
                editor.loadPlugin('image',
                    function() {
                        editor.plugin.imageDialog({
                            imageUrl: $('#Img').textbox('getValue'),
                            clickFn: function(url) {
                                $('#Img').textbox('setValue', url);
                                editor.hideDialog();
                            }
                        });
                    });
                return false;
            });
        });
    </script>
}
@section headSection
{
    <style>
        #news-form {
            margin-top: 20px;
        }

            #news-form .input-div {
                margin: 15px;
                display: block;
            }
    </style>
}
<div>
    @using (Ajax.BeginForm("PutNews", new { area = "Admin", controller = "News" }, new AjaxOptions()
    {
        HttpMethod = "Put",
        OnBegin = "doBegin",
        OnFailure = "doFail",
        OnComplete = "doComplete"
    }, new { id = "news-form", name = "news-form" }))
    {
        <input type="hidden" name="Id" value="@news.Id"/>
        <div class="input-div"><input id="Title" name="Title" value="@news.Title" class="easyui-textbox" data-options="label:'标题：',labelPosition:'left',labelWidth:100," style="width: 80%; height: 30px;"></div>
        <div class="input-div"><input id="Keyword" name="Keyword" value="@news.Keyword" class="easyui-textbox" data-options="label:'关键字：',labelPosition:'left',labelWidth:100," style="width: 80%; height: 30px;"></div>
        <div class="input-div"><input id="Img" name="Img" value="@news.Img"  class="easyui-textbox" data-options="label:'缩略图(可空)：',labelPosition:'left',labelWidth:120," style="width: 80%; height: 30px;"><button id="form-upimg" style="margin-left:10px" onclick="javascript: return false;" class="badge color-grayish">上传图片</button></div>
        <div class="input-div"><input id="Channels" name="Channels" value="@channel" class="easyui-textbox" data-options="label:'所属栏目：',labelPosition:'left',labelWidth:100," style="width: 65%; height: 30px;"></div>
        <div class="input-div"><input id="Content"  value="@sanitizer.Sanitize(news.Content)" data-options="multiline:true" name="Content" style="width: 98%; height: 500px"></div>
        <div class="input-div">
            <input type="submit" class="easyui-linkbutton button-blue l-btn l-btn-small" name="submit" style="width: 46px; height: 30px; margin: 10px" value="提交" />
            <input type="reset" class="easyui-linkbutton button-red l-btn l-btn-small" style="width: 46px; height: 30px; margin: 10px" name="reset" value="重置" />
        </div>
    }
</div>


